package com.hx.company.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hx.company.ManagerInterface;
import com.hx.company.UserInterface;
import com.hx.company.vo.Admin;
import com.hx.company.vo.Merchant;
import com.hx.company.vo.Order;
import com.hx.company.vo.User;
import com.hx.company.vo.UserLogin;




@Controller
public class UserMangerController {
	@Autowired(required=true)
	private  UserInterface ui;
	@Autowired(required=true)
	private  ManagerInterface mi;
	@RequestMapping("/test")
	@ResponseBody
	public User test11(){
		User s = new User();
		s.setUaddress("adress");
		s.setUname("join");
		s.setUphone("123");
		return s;
	}
	
	@RequestMapping("/adminLogin")
	public String adminLogin(Admin a,HttpServletRequest request){
		System.out.println("........"+a.getAname()+a.getApassword());
		Boolean i=mi.ManagerLogin(a);	
		if(i==true)
		return "redirect:/Aloginsucc.html";
		else return "adminlogin";
	}
	
	@RequestMapping("/addManager")
	@ResponseBody
	public Admin addManagers(Admin a,HttpServletRequest request){
		System.out.println("........"+a.getAname()+a.getApassword());
		Admin ad=mi.addManager(a);	
		System.out.println("success");
			return ad;
	}
	@RequestMapping("/getMerchants")
	@ResponseBody
	 public List<Merchant> getMerchantsss(){
		List<Merchant> me=new ArrayList<>();
		me=mi.getMerchant();
		System.out.println(me.size()+"###########");
		
		for(Merchant f:me){
			System.out.println(f.getMname()+"&&&&&&&&&&&&&&&&&&&&&");
		}
		return me;
	}
					
	@RequestMapping("/accMer")
	@ResponseBody
	 public boolean acceptMergent(int id){
		boolean m=mi.setMerchant(id);
		System.out.println(mi.getMerchant().get(0).getMid());
		return m;
	}	
	@RequestMapping("/deleteMerch")
	public String deleteMerch(int id){
		System.out.println("start");
		mi.deleteMerch(id);
		return "loginSuc";
	}
	@RequestMapping("/userRegist")
	public String uesrRegist(User u,UserLogin ul,HttpServletRequest request){
		System.out.println(u.getUname()+u.getUphone()+"........"+ul.getUphone()+ul.getUpassword());
		int rs=ui.userRegister(u,ul);	
		if(rs>0)
		return "redirect:/login.jsp";
		else return "regist1.jsp";
	}
	
	@RequestMapping("/userLogin")
	public String userLogin(UserLogin ul,HttpServletRequest request){
		System.out.println("........"+ul.getUphone()+ul.getUpassword());
		Boolean i=ui.userLogin(ul);	
		if(i==true)
		return "redirect:/loginSuc.jsp";
		else return "regist1.jsp";
	}
	
	
	@RequestMapping("/usergetMerchant")
	@ResponseBody
	public List<Merchant> getMerchant(){
	
		
		return ui.getMerchant();
		
	}
	
	@RequestMapping("/sendOrder")
	public Boolean sendOrder(Order o,HttpServletRequest request){
		
	return ui.sendOrder(o);	
	}
	

}
